package com.jimmyhsu.ecnudaowei.Db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.jimmyhsu.ecnudaowei.Bean.TaskActivity;
import com.jimmyhsu.ecnudaowei.coursetable.bulletin.Bulletin;
import com.jimmyhsu.ecnudaowei.coursetable.file.SharedFile;

/**
 * Created by jimmyhsu on 2016/10/27.
 */

public class UserDbHelper extends SQLiteOpenHelper {

    private static int DB_VERSION = 2;
    private static String DB_NAME = "big_fish_db";
    private UserDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    private static UserDbHelper mInstance;

    private static final String SQL_CREATE_USER_DB = "create table if not exists " + User.TB_NAME +
            "(_id integer primary key autoincrement, " +
            User.COL_NAME + " text not null, " +
            User.COL_AGE + " integer not null, " +
            User.COL_MOBILE + " text not null, " +
            User.COL_REGDATE + " text not null, " +
            User.COL_SEX + " integer not null, " +
            User.COL_STU_ID + " text not null, " +
            User.COL_USERINFO_ID + " integer not null, " +
            User.COL_SIGNATURE + " text not null," +
            User.COL_VERIFIED + " integer not null," +
            User.COL_REALNAME + " text not null" +
            ")";

    private static final String SQL_CREATE_COURSE_DB = "create table if not exists " + TaskActivity.TB_COURSE +
            "(_id integer primary key autoincrement, " +
            TaskActivity.COL_COURSE_ID + " text not null, " +
            TaskActivity.COL_COURSE_NAME + " text not null, " +
            TaskActivity.COL_LOCATION + " text not null, " +
            TaskActivity.COL_TEACHER + " text, " +
            TaskActivity.COL_DURATION + " integer default 1, " +
            TaskActivity.COL_COURSE_WEEK + " text not null, " +
            TaskActivity.COL_DAY + " integer not null, " +
            TaskActivity.COL_NUMBER + " integer not null, " +
            TaskActivity.COL_ADD_BY_USER + " int default 1" +
            ")";

    private static final String SQL_CREATE_BULLETIN_DB = "create table if not exists " + Bulletin.TABLE_NAME +
            "(_id integer primary key autoincrement, " +
            Bulletin.COL_COURSE_ID + " text not null, " +
            Bulletin.COL_B_ID + " integer not null, " +
            Bulletin.COL_TITLE + " text not null, " +
            Bulletin.COL_CONTENT + " text not null, " +
            Bulletin.COL_RELEASE_DATE + " integer not null, " +
            Bulletin.COL_AUTHOR + " text not null, " +
            Bulletin.COL_IMAGEURL + " text not null" +
            ")";

    private static final String SQL_CREATE_FILE_DB = "create table if not exists " + SharedFile.TABLE_NAME +
            "(_id integer primary key autoincrement, " +
            SharedFile.COL_COURSE_ID + " text not null, " +
            SharedFile.COL_URL + " text not null, " +
            SharedFile.COL_AUTHOR + " text not null, " +
            SharedFile.COL_SIZE + " integer not null, " +
            SharedFile.COL_TIME + " integer not null, " +
            SharedFile.COL_TYPE + " text not null, " +
            SharedFile.COL_TITLE + " text not null, " +
            SharedFile.COL_DOWNLOADED + " integer not null" +
            ")";

    public static UserDbHelper getInstance(Context context) {
        if (mInstance == null) {
            synchronized (UserDbHelper.class) {
                if (mInstance == null) {
                    mInstance = new UserDbHelper(context);
                }
            }
        }
        return mInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_USER_DB);
        db.execSQL(SQL_CREATE_COURSE_DB);
        db.execSQL(SQL_CREATE_BULLETIN_DB);
        db.execSQL(SQL_CREATE_FILE_DB);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion == 1) {
            db.execSQL("ALTER TABLE " + SharedFile.TABLE_NAME + " ADD COLUMN " + SharedFile.COL_DOWNLOADED + " integer");
            db.execSQL("UPDATE " + SharedFile.TABLE_NAME + " SET " + SharedFile.COL_DOWNLOADED + "=0");
        }
    }
}
